package ext.tianma.part.processors;

import ext.com.workflow.WorkflowUtil;
import ext.generic.erp.common.CommonUtil;
import ext.generic.erp.service.BOMReleaseService;
import ext.generic.erp.service.PartReleaseService;
import java.util.List;

import wt.part.WTPart;
import wt.util.WTException;

import com.ptc.core.components.forms.DefaultObjectFormProcessor;
import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.core.components.util.FeedbackMessage;
import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;

import ext.tianma.bom.workflow.ReWriteBOMReviewConfirmBusiness;
import ext.tianma.part.forms.TMTypeUtil;

public class TransTrialProductionProcessor extends DefaultObjectFormProcessor {
	
	private static final String NEW_PRODUCT = "NewProduct";
	private static final String APPROVAL = "APPROVAL";
	private static final String MASS_PRODUCTION_RELEASE = "MassProductionRelease";
	
	@Override
	public FormResult doOperation(NmCommandBean nmCommandBean, List list) throws WTException {
		
		
		FormResult formresult = new FormResult(FormProcessingStatus.SUCCESS);
		
		NmOid nmoid = nmCommandBean.getActionOid();
		WTPart currentPart = (WTPart) nmoid.getRefObject();
		if( currentPart != null ){
			
			if(TMTypeUtil.isFinishedGood(currentPart) || TMTypeUtil.isSemiFinishedGood(currentPart))
			{
				String state = currentPart.getLifeCycleState().toString();
				//转试产  入口打开  ||state.equals("NewProduct")
				if(state.equals("NewProduct"))
				{
					currentPart = (WTPart)WorkflowUtil.setLifeCycleState( currentPart, "TrialProduction" );
					List<WTPart> allPart = ReWriteBOMReviewConfirmBusiness.getAllSubPartsWithSubstitute(currentPart);
					for (WTPart wtPart : allPart) {
						if(TMTypeUtil.isFinishedGood(wtPart) || TMTypeUtil.isSemiFinishedGood(wtPart)){
							if(NEW_PRODUCT.equals(wtPart.getLifeCycleState().toString())){
								wtPart = (WTPart)WorkflowUtil.setLifeCycleState( wtPart, "TrialProduction" );
							}
						}
						else if(TMTypeUtil.isRawMaterial(wtPart)) {
							if(NEW_PRODUCT.equals(wtPart.getLifeCycleState().toString())||APPROVAL.equals(wtPart.getLifeCycleState().toString())){
								wtPart = (WTPart)WorkflowUtil.setLifeCycleState( wtPart, MASS_PRODUCTION_RELEASE );
							}
						}
						
						
					}
					BOMReleaseService.releaseAllLevel(currentPart);
				}
			}
		}
		FeedbackMessage feedbackmessage = new FeedbackMessage(
		FeedbackType.SUCCESS, null, "转试产成功", null, new String[] {});
		formresult.addFeedbackMessage(feedbackmessage);
		return formresult;
		
	}
	
}


